/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 12.09.12
 * Time: 10:15
 * To change this template use File | Settings | File Templates.
 */
package fgui.content {
import fgui.GuiComponent;

import fgui.GuiComponent;
import fgui.UIGraphicsComponent;
import fgui.control.DrawButton;

import fgui.control.ProgressBar;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;

import mx.controls.Label;

public class AudioContainer extends GuiComponent {
    private var sound:Sound;
    private var soundChannel_1:SoundChannel;
    private var soundChannel_2:SoundChannel;
    private var sTransform:SoundTransform;
    private var soundPosition:Number = 0;
    private var isPlaySound:Boolean = false;

    // ui
    private var lblTrackName:Label;
    private var btnPlayPause:DrawButton;
    private var progress:ProgressBar;
    private var volume:ProgressBar;

    public function AudioContainer(coordinate:Point, url:String) {
        super(coordinate, new Point(200, 30));

        renderColor = 0xdddddd;
        renderAlpha = 1;
        renderDefault();

        try {
            sound = new Sound(new URLRequest(url));
            soundChannel_1 = sound.play();
            soundChannel_1.stop();
        } catch (error:Error) {
            trace(error.message);
        }
        sTransform = new SoundTransform();

        sound.addEventListener(Event.COMPLETE, onSoundComplete);

        lblTrackName = new Label();
        lblTrackName.x = 10;
        lblTrackName.y = -15;
        lblTrackName.width = this.getSize().x;
        lblTrackName.height = 20;
        addChild(lblTrackName);

        btnPlayPause = new DrawButton(new Point(), new Point(30, 30));
        addChild(btnPlayPause);
        btnPlayPause.addEventListener(MouseEvent.CLICK, onClickPlayPauseButton);
        drawPlayButton();

        progress = new ProgressBar(new Point(35, 5), new Point(150, 10));
        progress.min = 0;
        progress.addEventListener(MouseEvent.CLICK, onChangeProgress);
        addChild(progress);

        volume = new ProgressBar(new Point(35, 20), new Point(80, 5));
        volume.min = 0;
        volume.max = 1;
        volume.addEventListener(MouseEvent.CLICK, onChangeVolume);
        addChild(volume);

        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private function onSoundComplete(event:Event):void {
        lblTrackName.text = sound.id3.songName;
        progress.max = sound.length;
        progress.value = 0;
    }

    private function onChangeProgress(event:MouseEvent):void {
        //trace();
        progress.onClickProgressBar(event);
        soundPosition = progress.value;
        if (isPlaySound) {
            soundChannel_2 = sound.play(soundPosition);
            soundChannel_1.stop();
            soundPosition = soundChannel_2.position;
            soundChannel_1 = sound.play(soundPosition);
            soundChannel_2.stop();
        }
        else
            soundChannel_1.stop();
    }

    private function onChangeVolume(event:MouseEvent):void {
        soundPosition = progress.value;
        volume.onClickProgressBar(event);
        sTransform.volume = volume.value;

        soundChannel_1.soundTransform = sTransform;
        if (isPlaySound) {
            soundChannel_1.stop();
            soundChannel_1 = sound.play(soundPosition);
            soundChannel_1.soundTransform = sTransform;
        }
    }

    private function onEnterFrame(event:Event):void {
        if (isPlaySound) {
            if (soundPosition == sound.length)
                stopSound();

            soundPosition = soundChannel_1.position;
            progress.value = soundChannel_1.position;
        }
    }

    private function onClickPlayPauseButton(event:MouseEvent):void {
        if (!isPlaySound)
            playSound();
        else
            pauseSound();
    }

    private function playSound():void {
        isPlaySound = true;
        drawPauseButton();
        soundChannel_1 = sound.play(soundPosition);
    }

    private function pauseSound():void {
        isPlaySound = false;
        drawPlayButton();
        soundPosition = soundChannel_1.position;
        soundChannel_1.stop();
    }

    private function stopSound():void {
        pauseSound();
        soundPosition = 0;
    }

    private function drawPlayButton():void {
        btnPlayPause.canvas.graphics.clear();
        btnPlayPause.canvas.graphics.beginFill(0xff0000);
        btnPlayPause.canvas.graphics.moveTo(this.btnPlayPause.getSize().x / 4, this.btnPlayPause.getSize().y / 4);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 4 * 3, this.btnPlayPause.getSize().y / 2);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 4, this.btnPlayPause.getSize().y / 4 * 3);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 4, this.btnPlayPause.getSize().y / 4);
        btnPlayPause.canvas.graphics.endFill();
    }

    private function drawPauseButton():void {
        btnPlayPause.canvas.graphics.clear();
        btnPlayPause.canvas.graphics.beginFill(0xff0000);

        btnPlayPause.canvas.graphics.moveTo(this.btnPlayPause.getSize().x / 4, this.btnPlayPause.getSize().y / 4);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 8 * 3, this.btnPlayPause.getSize().y / 4);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 8 * 3, this.btnPlayPause.getSize().y / 4 * 3);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 4, this.btnPlayPause.getSize().y / 4 * 3);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 4, this.btnPlayPause.getSize().y / 4);

        btnPlayPause.canvas.graphics.moveTo(this.btnPlayPause.getSize().x / 8 * 5, this.btnPlayPause.getSize().y / 4);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 8 * 6, this.btnPlayPause.getSize().y / 4);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 8 * 6, this.btnPlayPause.getSize().y / 4 * 3);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 8 * 5, this.btnPlayPause.getSize().y / 4 * 3);
        btnPlayPause.canvas.graphics.lineTo(this.btnPlayPause.getSize().x / 8 * 5, this.btnPlayPause.getSize().y / 4);

        btnPlayPause.canvas.graphics.endFill();
    }
}
}
